package com.inno.thread.inno.pipe;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Piped {
    public static void main(String[] args) throws IOException {
        PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream();
        out.connect(in);

        Thread printThread = new Thread(new Print(in), "printThread");
        printThread.start();
        int receive = 0;
        try {
            while ((receive = System.in.read()) != -1) {
                out.write(receive);
            }
        } finally {
            out.close();
        }


    }

    static class Print implements Runnable {
        private PipedInputStream in;

        public Print(PipedInputStream in) {
            this.in = in;
        }

        @Override
        public void run() {
            int length = 0;
            byte[] content = new byte[1024];
            while (true) {
                try {
                    if (((length = in.read(content)) != -1)) {
                        System.out.println("你输入了： " + new String(content, "utf-8"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
